#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 5;
const int M = 1e9 + 7;

int t, n;
int a[N], b[N];
map<int, int> m;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);

    cin >> t;
    while(t--) {
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i], m[a[i]] = i;
        for (int i = 1; i <= n; i++) cin >> b[i], m[b[i]] = i;

        int res = n*2;
        for (int i = 1, j = 1; i <= n; i++) {
            while (b[j] < i*2-1) j++;
            res = min(res, m[i*2-1]+j-2);
        }
        cout << res << endl;
    }
    return 0;
}